package car.aistar.controller.uploads;

import car.aistar.dto.Result;
import car.aistar.entity.CarDetail;
import car.aistar.entity.Picture;
import car.aistar.service.CarDetailService;
import car.aistar.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 本类用于：
 *
 * @author 许华钢
 * @Classname uploadCar
 * @Description TODO
 * @date 2020/10/13 10:23
 */
@Controller
@RequestMapping("/uploadCar")
public class uploadCar {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private CarDetailService carDetailService;

    /** 上传车辆封面
     * @author 许华钢
     * @param req
     * @param file
     * @return
     */
//    @PostMapping("/carMain/nginx/{carID}")
    @PostMapping("/carMain/nginx/{carID}")
    public @ResponseBody Result uploadCarMain(HttpServletRequest req, @RequestParam("file") MultipartFile file,@PathVariable("carID") String carID){
        //重新进行命名
        String str = file.getOriginalFilename();
        str= str.substring(str.indexOf("."));
//        System.err.println("图片类型："+str);
        String fileName = carID+str;

//        创建图片对象存储车辆图片
        Picture newPt = new Picture();
        newPt.setCid(Long.valueOf(carID));
        newPt.setPictureDesc(carDetailService.queryById(Long.valueOf(carID)).getCDesc());
        newPt.setPictureUrl("http://192.168.4.98/carJY/carMain/"+fileName);
//        图片对象插入数据库
        pictureService.insert(newPt);
//        更新数据库车辆详情表关于当前车辆的封面
        CarDetail car = carDetailService.queryById(Long.valueOf(carID));
        car.setCPhoto("http://192.168.4.98/carJY/carMain/"+fileName);
        carDetailService.update(car);

        //构建File实例
        File f = new File("F:/codeSofe/nginx-1.16.1/nginx-1.16.1/html/carJY/carMain/"+fileName);

        //如果需要把图片上传到nginx
        //a-nginx和当前项目在同一个计算机中 - file -> html的绝对路径
        //b-nginx如何和当前项目不再同一个计算机中 - commons-net

        //写入到磁盘中
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Result("200","文件上传成功!",fileName);
    }

    /**
     * @author 许华钢
     * @param file
     * @return
     */
    @PostMapping("/carDesc/addOne")
    public @ResponseBody Result uploadCarDesc(@RequestParam("file") MultipartFile file,String carId){
        //重新进行命名
        String str = file.getOriginalFilename();
        str= str.substring(str.indexOf("."));
//        System.err.println("图片类型："+str);
        String fileName = carId+"_"+UUID.randomUUID().toString()+str;
//        String fileName = UUID.randomUUID().toString()+"_"+file.getOriginalFilename();

        // 根据cid取出对应车辆图片对象
        Picture newPhoto = new Picture();
        newPhoto.setCid(Long.valueOf(carId));
        List<Picture> pictures = pictureService.queryAll(newPhoto);

        Picture newPt = pictures.get(0);
//        System.err.println(newPt.getFen1Url());
        if (null==newPt.getFen1Url()){
            newPt.setFen1Url("http://192.168.4.98/carJY/carDesc/"+fileName);
            // 图片对象插入数据库
            pictureService.update(newPt);
        }
        if (null==newPt.getFen2Url()){
            newPt.setFen2Url("http://192.168.4.98/carJY/carDesc/"+fileName);
            // 图片对象插入数据库
            pictureService.update(newPt);
        }
        if (null==newPt.getFen3Url()){
            newPt.setFen3Url("http://192.168.4.98/carJY/carDesc/"+fileName);
            // 图片对象插入数据库
            pictureService.update(newPt);
        }
        if (null==newPt.getFen4Url()){
            newPt.setFen4Url("http://192.168.4.98/carJY/carDesc/"+fileName);
            // 图片对象插入数据库
            pictureService.update(newPt);
        }


        //构建File实例
        File f = new File("F:/codeSofe/nginx-1.16.1/nginx-1.16.1/html/carJY/carDesc/"+fileName);

        //如果需要把图片上传到nginx
        //a-nginx和当前项目在同一个计算机中 - file -> html的绝对路径
        //b-nginx如何和当前项目不再同一个计算机中 - commons-net

        //写入到磁盘中
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Result("200","文件上传成功!",fileName);
    }

    @PostMapping("/carJyDesc/addMany/nginx/{carID}")
    public @ResponseBody Result uploadCarDescAddMany(HttpServletRequest req, @RequestParam("file") MultipartFile[] files,@PathVariable("carID") String carID){

        String[] path = new String[files.length];
        if (files!=null&&files.length!=0){
            int i = 0;
            for (MultipartFile ff : files) {
                path[i++]=uploadCarDesc(ff,carID).getData().toString();
            }
        }

        return new Result("200","多图片成功上传",path);
    }

}
